package com.yc.scott.biz;

import java.util.Map;

import com.yc.jdbc.DBHelper;
import com.yc.scott.dao.EmpDao;
import com.yc.scott.util.Utils;

public class EmpBiz {
	
	private static Map<String,Object> loginedUser;
	
	private EmpDao dao = new EmpDao();
	
	public Map<String,Object> login(String name, String pwd) throws BizException{
		if(name == null || name.isBlank()) {
			throw new BizException("请输入用户名");
		}
		if(pwd == null || pwd.isBlank()) {
			throw new BizException("请输入密码");
		}
		
		loginedUser = DBHelper.selectOne("select * from emp"
				+ " where job in ('MANAGER','PRESIDENT')"
				+ "   and ename=? and pwd=?", 
							name, pwd);
		
		if(loginedUser != null) {
			System.out.println("登录成功!");
			return loginedUser;
		} else {
			throw new BizException("用户名或密码错误");
		}
	}

	public static Map<String, Object> getLoginedUser() {
		return loginedUser;
	}
	
	public void create(String ename, String job, Integer mgr,
			java.sql.Date hiredate, Integer sal, Integer comm, Integer deptno,
			String pwd, String repwd) throws BizException {
		check(ename, job,mgr,hiredate,sal,comm,deptno,pwd,repwd);
		dao.insert(ename, job, mgr, hiredate, sal, comm, deptno, pwd);
	}
	
	public void modify(Integer empno, String ename, String job, Integer mgr,
			java.sql.Date hiredate, Integer sal, Integer comm, Integer deptno,
			String pwd, String repwd) throws BizException {
		check(ename, job,mgr,hiredate,sal,comm,deptno,pwd,repwd);
		dao.update(empno, ename, job, mgr, hiredate, sal, comm, deptno, pwd);
	}
	
	private void check(String ename, String job, Integer mgr,
			java.sql.Date hiredate, Integer sal, Integer comm, Integer deptno,
			String pwd, String repwd) throws BizException {
		Utils.checkEmpty(ename, "请输入用户名");
		Utils.checkEmpty(pwd, "请输入密码");
		Utils.checkEmpty(repwd, "请输入确认密码");
		Utils.check(pwd.equals(repwd) ==  false, "两次输入的密码不一致");
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	

}
